package com.ssm.driver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ssm.common.utils.PageUtils;
import com.ssm.driver.dao.SsmLiushuiDao;
import com.ssm.driver.entity.SsmLiushuiEntity;
import com.ssm.driver.entity.SsmSjEntity;
import com.ssm.driver.entity.SsmSyTypeEntity;
import com.ssm.driver.entity.vo.LiuShuiDto;
import com.ssm.driver.service.SsmLiushuiService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;


@Service("ssmLiushuiService")
@AllArgsConstructor
public class SsmLiushuiServiceImpl extends ServiceImpl<SsmLiushuiDao, SsmLiushuiEntity> implements SsmLiushuiService {
    private final SsmLiushuiDao ssmLiushuiDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<SsmLiushuiEntity> page = this.page(
//                new Query<SsmLiushuiEntity>().getPage(params),
//                new QueryWrapper<SsmLiushuiEntity>()
//        );
        if (params.get("key")!=null&&StringUtils.isNotBlank(params.get("key").toString())){
            String condition = (String) params.get("key");
            IPage<LiuShuiDto> page = ssmLiushuiDao.selectJoinPage(new Page<>(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString())),
                    LiuShuiDto.class,
                    new MPJLambdaWrapper<SsmLiushuiEntity>()
                            .selectAll(SsmLiushuiEntity.class)
                            .selectAs(SsmSjEntity::getSjName,LiuShuiDto::getSjName)
                            .selectAs(SsmSjEntity::getSjPhone,LiuShuiDto::getSjPhone)
                            .selectAs(SsmSyTypeEntity::getSyName, LiuShuiDto::getSyName)
                            .leftJoin(SsmSjEntity.class,SsmSjEntity::getSjId,LiuShuiDto::getSjId)
                            .leftJoin(SsmSyTypeEntity.class,SsmSyTypeEntity::getSyId,LiuShuiDto::getSyType)
                            .like(SsmSjEntity::getSjName,condition));
            return new PageUtils(page);
        }
        IPage<LiuShuiDto> page2 = ssmLiushuiDao.selectJoinPage(new Page<>(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString())),
                LiuShuiDto.class,
                new MPJLambdaWrapper<SsmLiushuiEntity>()
                        .selectAll(SsmLiushuiEntity.class)
                        .selectAs(SsmSjEntity::getSjName,LiuShuiDto::getSjName)
                        .selectAs(SsmSjEntity::getSjPhone,LiuShuiDto::getSjPhone)
                        .selectAs(SsmSyTypeEntity::getSyName, LiuShuiDto::getSyName)
                        .leftJoin(SsmSjEntity.class,SsmSjEntity::getSjId,LiuShuiDto::getSjId)
                        .leftJoin(SsmSyTypeEntity.class,SsmSyTypeEntity::getSyId,LiuShuiDto::getSyType));

        return new PageUtils(page2);
    }

    @Override
    public ArrayList<LiuShuiDto> getBysjId(int parseInt) {
        return ssmLiushuiDao.selectLiushui(parseInt);
    }

}